package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.zd.demo1.broadcast.MyAlarmReceiver;
import cn.zd.demo1.broadcast.MyBroadCastConstant;
import cn.zd.demo1.broadcast.MyBroadCastReceiver;
import cn.zd.demo1.broadcast.MyNetChangeReceiver;
import cn.zd.demo1.broadcast.MyStaticReceiver;
import cn.zd.demo1.broadcast.MyTimerReceiver;
import cn.zd.demo1.broadcast.OrderAReceiver;
import cn.zd.demo1.broadcast.OrderBReceiver;

public class BroadcastActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_1;
    private EditText et_1;
    private CheckBox ck_1;
    private MyBroadCastReceiver myBroadCastReceiver;
    private OrderBReceiver receiverB;
    private OrderAReceiver receiverA;
    private MyTimerReceiver myTimerReceiver;
    private MyNetChangeReceiver myNetChangeReceiver;
    private MyAlarmReceiver myAlarmReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
        Toast.makeText(this, this.getClass().getName(), Toast.LENGTH_LONG).show();
        tv_1 = findViewById(R.id.tv_1);
        et_1 = findViewById(R.id.et_1);
        ck_1 = findViewById(R.id.ck_1);
        findViewById(R.id.btn_send1).setOnClickListener(this);
        findViewById(R.id.btn_send2).setOnClickListener(this);
        findViewById(R.id.btn_send3).setOnClickListener(this);
        findViewById(R.id.btn_send4).setOnClickListener(this);
        findViewById(R.id.btn_reg).setOnClickListener(this);
        findViewById(R.id.btn_unreg).setOnClickListener(this);
        findViewById(R.id.btn_reg1).setOnClickListener(this);
        findViewById(R.id.btn_unreg1).setOnClickListener(this);
        findViewById(R.id.btn_reg2).setOnClickListener(this);
        findViewById(R.id.btn_unreg2).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_reg) {
            // 标准广播
            // 创建过滤器
            IntentFilter filter = new IntentFilter(MyBroadCastConstant.ACTION);
            // 创建接收器
            myBroadCastReceiver = new MyBroadCastReceiver(this);
            // 注册广播
            registerReceiver(myBroadCastReceiver,filter);
            Toast.makeText(this,"注册标准广播"+MyBroadCastConstant.ACTION,Toast.LENGTH_SHORT).show();

            // 注册有序广播
            // 1.优先级越大越先收到广播
            // 2. 优先级相同时，越早注册越早收到广播
            receiverA = new OrderAReceiver(this);
            IntentFilter filterA = new IntentFilter(MyBroadCastConstant.ACTION_ORDER);
            // 设置优先级
            filterA.setPriority(10);
            receiverB = new OrderBReceiver(this);
            IntentFilter filterB = new IntentFilter(MyBroadCastConstant.ACTION_ORDER);
            // 设置优先级
            filterB.setPriority(8);
            registerReceiver(receiverA,filterA);
            registerReceiver(receiverB,filterB);
            Toast.makeText(this,"注册有序广播"+MyBroadCastConstant.ACTION_ORDER,Toast.LENGTH_SHORT).show();


        } else if(v.getId() == R.id.btn_unreg) {
            unregisterReceiver(myBroadCastReceiver);
            Toast.makeText(this,"取消注册广播"+MyBroadCastConstant.ACTION,Toast.LENGTH_SHORT).show();
            unregisterReceiver(receiverA);
            unregisterReceiver(receiverB);
            Toast.makeText(this,"取消注册广播"+MyBroadCastConstant.ACTION_ORDER,Toast.LENGTH_SHORT).show();
        } else if(v.getId() == R.id.btn_send1) {
            // 标准广播
            Intent intent = new Intent(MyBroadCastConstant.ACTION);
            intent.putExtra("data",et_1.getText().toString());
            sendBroadcast(intent);
        }else if(v.getId() == R.id.btn_send2) {
            // 有序广播
            Intent intent = new Intent(MyBroadCastConstant.ACTION_ORDER);
            intent.putExtra("data",et_1.getText().toString());
            intent.putExtra("abort",ck_1.isChecked());
            sendOrderedBroadcast(intent,null);
        } else if(v.getId() == R.id.btn_send3) {
            // 静态广播在 Manifest.xml设置
            Toast.makeText(this,"发送静态广播"+MyBroadCastConstant.ACTION_ORDER,Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MyBroadCastConstant.ACTION_SOCK);
            // 8.0 以后需要指定接收者的完整类名，所以8.0以后就不再建议使用静态注册了（详见 Manifest.xml设置）
            ComponentName component = new ComponentName(this, "cn.zd.demo1.broadcast.MyStaticReceiver");
            intent.setComponent(component);
            sendBroadcast(intent);
        }else if(v.getId() == R.id.btn_send4) {
            myAlarmReceiver.sendAlarm();
            Toast.makeText(this,"发送Alarm广播"+MyBroadCastConstant.ACTION_ORDER,Toast.LENGTH_SHORT).show();
        }  else if(v.getId() == R.id.btn_reg1) {
            myTimerReceiver = new MyTimerReceiver();
            IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
            registerReceiver(myTimerReceiver,filter);
            Toast.makeText(this,"注册系统分钟广播",Toast.LENGTH_SHORT).show();
        } else if(v.getId() == R.id.btn_unreg1) {
            unregisterReceiver(myTimerReceiver);
            Toast.makeText(this,"取消注册系统分钟广播",Toast.LENGTH_SHORT).show();
        } else if(v.getId() == R.id.btn_reg2) {
            myNetChangeReceiver = new MyNetChangeReceiver();
            IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
            registerReceiver(myNetChangeReceiver,filter);
            Toast.makeText(this,"注册系统网络变更广播",Toast.LENGTH_SHORT).show();
        } else if(v.getId() == R.id.btn_unreg2) {
            unregisterReceiver(myNetChangeReceiver);
            Toast.makeText(this,"取消注册系统网络变更广播",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(myBroadCastReceiver);
        unregisterReceiver(receiverA);
        unregisterReceiver(receiverB);
        unregisterReceiver(myTimerReceiver);
        unregisterReceiver(myAlarmReceiver);
        Toast.makeText(this, "取消注册广播", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        myAlarmReceiver = new MyAlarmReceiver(getApplicationContext());
        IntentFilter intentFilter = new IntentFilter(MyBroadCastConstant.ACTION_ALARM);
        registerReceiver(myAlarmReceiver,intentFilter);
        Toast.makeText(this, "注册Alarm广播" + MyBroadCastConstant.ACTION, Toast.LENGTH_SHORT).show();

    }
}